package top.ffhome.util.reflect;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import cn.hutool.core.exceptions.UtilException;

public class ReflectUtil extends cn.hutool.core.util.ReflectUtil {
	/**
	 * 生成类中泛型示例
	 * @param clazz
	 * @return
	 * @throws UtilException
	 */
	@SuppressWarnings("unchecked")
	public static <T> T newParameterizedTypeInstance(Class<?> clazz) throws UtilException {
		try {
			Type type = clazz.getGenericSuperclass();
			ParameterizedType pt = (ParameterizedType) type;
			Class<?> modelClass = (Class<?>) pt.getActualTypeArguments()[0];
			return (T) modelClass.getConstructor().newInstance();
		} catch (Exception e) {
			throw new UtilException(e, "Instance class [{}] error!", clazz);
		}
	}
}
